const path = require('path');
const dotenv = require('dotenv');
const webpack = require('webpack');

// 根据环境加载不同的.env文件
dotenv.config({
  path: path.resolve(
    __dirname,
    `.env.${process.env.NODE_ENV || 'development'}`
  )
});

module.exports = {
  devServer: {
    proxy: {
      '/admin3': {
        target: 'http://127.0.0.1:8080',
        changeOrigin: true,
        pathRewrite: {
          '^/admin3': '/admin3'
        }
      }
    }
  },
  // 更安全的环境变量注入方式
  chainWebpack: (config) => {
    config.plugin('define').use(webpack.DefinePlugin, [{
      'process.env': JSON.stringify(process.env)
    }]);
  },
  configureWebpack: {
    resolve: {
      extensions: ['.ts', '.js', '.vue', '.json'],
      alias: {
        '@popperjs/core$': path.resolve(__dirname, 'node_modules/@popperjs/core/dist/umd/popper.min.js'),
        '@ctrl/tinycolor$': path.resolve(__dirname, 'node_modules/@ctrl/tinycolor/dist/module/index.js'),
        '@floating-ui/dom$': path.resolve(__dirname, 'node_modules/@floating-ui/dom/dist/floating-ui.dom.mjs')
      }
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          loader: 'ts-loader',
          options: { 
            appendTsSuffixTo: [/\.vue$/],
            transpileOnly: true // 添加这个选项可以避免一些解析问题
          }
        }
      ]
    }
  }
}
